//
//  SSNormalTimeViewController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/6/2.
//

import UIKit


class SSNormalTimeViewController: UIViewController {
    
    var refresh: SSCallBack?     // 闭包

    @IBOutlet weak var tagLab: UILabel!
    @IBOutlet weak var priceLab: UILabel!
    
    @IBOutlet weak var titleLab: UILabel!
    
    @IBOutlet weak var selectedDesLab: UILabel!
    
    @IBOutlet weak var imageV: UIImageView!
    
    @IBOutlet weak var tagBGView: UIView!
    
    @IBOutlet weak var closeBtn: UIButton!
    
    var lastSelectedLab = UILabel()
    var normal:SSNormal = SSNormal()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        closeBtn.ssc_expandSize(size: 50)
        
        setupData()
    }
    
    func refresh(_ finished: @escaping SSCallBack) {
        refresh = finished
    }
    
    func setupData() {
        tagLab.text = normal.pkgType
        imageV.setImage(with: normal.pkgImg)
        guard let price = normal.listSpecs.first?.specsPrice else{
            return
        }
        priceLab.text = "$\(price)"
        
        //标题处理
        titleLab.text = normal.pkgName
        var describeStr = ""
        
        for item in normal.listSpecs{
            if(item.isSelected){
                describeStr = item.specsDescribe
            }
            
        }
        selectedDesLab.text = "已选择：\(describeStr)"
        
        
        //处理标签
        for v in tagBGView.subviews{
            v.removeFromSuperview()
        }
        
        var lastLab = UILabel()
        
        for (index,item) in normal.listSpecs.enumerated(){
            let lab = UILabel()
            if(item.isSelected){
                self.lastSelectedLab = lab
                setupSelectedLab(lab: lab)
                lab.backgroundColor = UIColor(hex: "E0EDF9")
                lab.layer.borderWidth = 2
                lab.layer.borderColor = UIColor(hex: "#197FD6")?.cgColor
                lab.textColor = UIColor(hex: "#197FD6")
            }else{
                setupLastLab(lab: lab)
                lab.backgroundColor = UIColor.white
                lab.layer.borderWidth = 1
                lab.layer.borderColor = UIColor(hex: "#F3F3F3")?.cgColor
                lab.textColor = UIColor(hex: "#3D3D3D")
            }
            lab.isUserInteractionEnabled = true
            lab.layer.cornerRadius = 4
            lab.layer.masksToBounds = true
            lab.tag = index + 100
            lab.text = item.specsDescribe
            lab.textAlignment = .center
            lab.font = UIFont.systemFont(ofSize: 12)
            var width = item.specsDescribe.sizeWithText(font: lab.font, size: CGSize(width: CGFloat.zero, height: 40)).width
            width = width + 40
            lab.frame = CGRect(x: lastLab.frame.maxX + 4, y: 0, width: width, height: 40)
            tagBGView.addSubview(lab)
            lastLab = lab
            
            let tapGes = UITapGestureRecognizer(target: self, action: #selector(tapedTitleLabel(_:)))
            lab.addGestureRecognizer(tapGes)
        }
        
    }

    
    @objc private func tapedTitleLabel(_ tapGes : UITapGestureRecognizer) {
        
        let lab = tapGes.view as! UILabel
        if(lab == lastSelectedLab){
            return
        }
        setupLastLab(lab: lastSelectedLab)
        setupSelectedLab(lab: lab)
        lastSelectedLab = lab
        
        
    }
    
    func setupSelectedLab(lab:UILabel) {
      
        lab.backgroundColor = UIColor(hex: "E0EDF9")
        lab.layer.borderWidth = 2
        lab.layer.borderColor = UIColor(hex: "#197FD6")?.cgColor
        lab.textColor = UIColor(hex: "#197FD6")
    }
    func setupLastLab(lab:UILabel) {
        lab.backgroundColor = UIColor.white
        lab.layer.borderWidth = 1
        lab.layer.borderColor = UIColor(hex: "#F3F3F3")?.cgColor
        lab.textColor = UIColor(hex: "#3D3D3D")
        
        
    }
 

    @IBAction func closeAction(_ sender: Any) {
        
        print("222")
        dismiss(animated: true)
    }
    
    @IBAction func bugAction(_ sender: Any) {
//        normal.listSpecs[lastSelectedLab.tag]
        
        normal.listSpecs[lastSelectedLab.tag - 100].isSelected = true
        
        for(index,item) in normal.listSpecs.enumerated(){
            if(item.isSelected){
                item.isSelected = false
            }
            if(index == (lastSelectedLab.tag - 100)){
                item.isSelected = true
            }
        }
        
        refresh!()
        print("11")
        dismiss(animated: true)
    }
    
}
